package com.restaurant.springboot.service.impl;
import com.restaurant.springboot.entity.DishesEntity;
import com.restaurant.springboot.entity.ShoppingCartEntity;
import com.restaurant.springboot.entity.ShoppingCartEntityPK;
import com.restaurant.springboot.entity.vo.ShopcartVO;
import com.restaurant.springboot.repositories.DishesRepository;
import com.restaurant.springboot.repositories.ShoppingCartRepository;
import com.restaurant.springboot.service.ShopcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
/**
 * @Author jack
 * @Date:2019/7/29 15:40
 */
@Service
public class ShopcartServiceImpl implements ShopcartService {

    @Autowired
    ShoppingCartRepository shoppingCartRepository;

    @Autowired
    DishesRepository dishesRepository;

    public List<ShoppingCartEntity> getUserAllShopcart(int openId){
        List<ShoppingCartEntity> allByUserId = shoppingCartRepository.findAllByUserId(openId);
        return allByUserId;
    }

    @Override
    public List<ShopcartVO> getUserAllShopcartVo(int openId){
        List<ShoppingCartEntity> allByUserId = shoppingCartRepository.findAllByUserId(openId);
        List<ShopcartVO> shopcartVOS = new ArrayList<>();
        allByUserId.forEach((e)->{
            ShopcartVO shopcartVO = new ShopcartVO();
            DishesEntity dishesEntity = dishesRepository.findById(e.getDishesId()).get(); //may be null point exception
            shopcartVO.setDish(dishesEntity);
            shopcartVO.setNumber(e.getNumber());
            shopcartVO.setUserId(e.getUserId());
            shopcartVOS.add(shopcartVO);
        });
        return shopcartVOS;
    }

    @Override
    public ShoppingCartEntity addShopcart(ShoppingCartEntity shoppingCart){
        ShoppingCartEntity shoppingCart1 = shoppingCartRepository.save(shoppingCart);
        return shoppingCart1;
    }

    @Override
    public boolean updateShopcart(ShoppingCartEntity shoppingCart){
        ShoppingCartEntityPK shoppingCartEntityPK = new ShoppingCartEntityPK();
        shoppingCartEntityPK.setUserId(shoppingCart.getUserId());
        shoppingCartEntityPK.setDishesId(shoppingCart.getDishesId());
        Optional<ShoppingCartEntity> result = shoppingCartRepository.findById(shoppingCartEntityPK);
        if(result.isPresent()){
            shoppingCartRepository.save(shoppingCart);
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean deleteShopcart(ShoppingCartEntity shoppingCart){
        ShoppingCartEntityPK shoppingCartEntityPK = new ShoppingCartEntityPK();
        shoppingCartEntityPK.setUserId(shoppingCart.getUserId());
        shoppingCartEntityPK.setDishesId(shoppingCart.getDishesId());
        Optional<ShoppingCartEntity> result = shoppingCartRepository.findById(shoppingCartEntityPK);
        if(result.isPresent()){
            shoppingCartRepository.delete(result.get());
            return true;
        }else{
            return false;
        }
    }
}
